package ch.ffhs.ks.storage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import ch.ffhs.ks.model.MailConfiguration;

public class StorageManager {

	public void saveConfiguration(MailConfiguration mailConfig)
	{
		try
		{

			FileOutputStream fileOut = new FileOutputStream("config.ser");
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(mailConfig);
			out.close();
			fileOut.close();

		}catch(IOException i)
		{
			i.printStackTrace();
		}
	}

	public MailConfiguration loadMailConfig()
	{
		try{
			File file = new File("config.ser");

			if(file.exists())
			{
				FileInputStream fileOut = new FileInputStream("config.ser");
				ObjectInputStream out = new ObjectInputStream(fileOut);


				MailConfiguration mailConfig = (MailConfiguration) out.readObject();

				out.close();
				fileOut.close();

				return mailConfig;
			}else{
				MailConfiguration mailConfig = new MailConfiguration();

				saveConfiguration(mailConfig);

				return mailConfig;
			}

		}catch(IOException i)
		{
			i.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

}
